package extensions.prototipador;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import extensions.prototipador.sprites.Sprite;
import extensions.prototipador.undo.UndoTool;

/**
 * Contiene todos los sprites que forman un prototipo.
 * 
 * @author Manu
 *
 */
public class Prototype
{
	
	private List<Sprite> _sprites;
	private int _elementsSize = 0;
	private String _name = null;
	
	private UndoTool _undoTool = new UndoTool(this);

	public Prototype(String name)
	{
		this();

		_name = name;	
	}
	
	public Prototype()
	{
		_sprites = new ArrayList<Sprite>();	
	}
	
	public boolean hasName()
	{
		return _name != null;
	}

	public void add(Sprite sprite)
	{
		synchronized (_sprites) {
			_sprites.add(sprite);
			_elementsSize = _sprites.size();
		}
	}
	
	public void addAll(Sprite[] sprites) {
		synchronized (_sprites) {
			for (Sprite s : sprites)
				_sprites.add(s);
			_elementsSize = _sprites.size();
		}
	}
	
	public void remove(Sprite sprite)
	{
		synchronized (_sprites) {
			_sprites.remove(sprite);
			_elementsSize = _sprites.size();
		}
	}
	
	public void removeAll(Sprite[] sprites)
	{
		synchronized (_sprites) {
			for (Sprite s : sprites)
				_sprites.remove(s);
			
			_elementsSize = _sprites.size();
		}
	}
	
	public int size()
	{
		return _elementsSize;
	}
	
	public UndoTool getUndoTool()
	{
		return _undoTool;
	}
	
	public void tick(long dt)
	{
		synchronized (_sprites) {
			for (Sprite e : _sprites)
			{
				e.tick(dt);
			}
		}
	}
	
	public void doDraw(long dt, Canvas canvas)
	{
        synchronized (_sprites) {
	        for (Sprite e : _sprites)
	        	e.doDraw(canvas);
		}
	}
	
	public Sprite getSpriteIn(int x, int y)
	{
		for (Sprite s : _sprites)
		{
			if (s.contains(x, y))
			{
				return s;
			}
		}
		
		return null;
	}
	
	public void clear()
	{
		synchronized (_sprites) {
			_sprites.clear();
		}
	}

	public String getName()
	{
		return _name;
	}
	
	public void setName(String name)
	{
		_name = name;
	}

	public Sprite[] getAllSprites() {
		return _sprites.toArray(new Sprite[0]);
	}

	public void doDetailDraw(long dt, Canvas canvas) {
        synchronized (_sprites) {
	        for (Sprite e : _sprites)
	        	e.doDetailDraw(canvas);
		}
	}

}
